<template>
	<view class="container">
		<mescroll-body @init="mescrollInit" :down="downOption" @down="downCallback" :up="upOption" @up="upCallback">
			<search-box @gotoSearch="gotoSearch"></search-box>
			<yxyl-swiper :swiperItems="swiperItems"></yxyl-swiper>
			<view><yxyl-nav v-for="(item, index) in navs" :key="index" :navs="item"></yxyl-nav></view>
			<view class="common-list">
				<block v-for="(item, index) in pageGeneral" :key="index"><yxyl-common :type="item.itemStyle" :rowData="item.items"></yxyl-common></block>
			</view>
			<view class="common-list">
				<block v-for="(item, index) in moreItems" :key="index"><yxyl-common :type="item.itemStyle" :rowData="item.items"></yxyl-common></block>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
import yxylSwiper from '@/components/yxyl-swiper/yxyl-swiper.vue';
import yxylNav from '@/components/yxyl-nav/yxyl-nav.vue';
import yxylCommon from '@/components/yxyl-common/yxyl-common.vue';
import MescrollBody from 'mescroll-uni/mescroll-body.vue'
import MescrollMixin from "mescroll-uni/mescroll-mixins.js";
import searchBox from '@/components/search-box/search-box.vue';
export default {
	mixins: [MescrollMixin],
	components: { yxylSwiper, MescrollBody, yxylNav, yxylCommon, searchBox },
	data() {
		return {
			swiperItems: [],
			navs: [],
			pageGeneral: [],
			moreItems: [],
			downOption: {
				use: true, // 是否启用下拉刷新; 默认true,
				auto: false,
				native:true
			},
			//上拉加载的常用配置
			upOption: {
				use: true, // 是否启用上拉加载; 默认true
				auto: false, // 是否在初始化完毕之后自动执行上拉加载的回调; 默认true

				noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
				empty: false,
				textNoMore: '---没有了---'
			}
		};
	},
	onLoad() {		
		uni.setNavigationBarTitle({
			title:this.$store.state.user.miniProgramTitle
		})
		this.updateInfo();
		this.getHomePage().then(() => {	
			this.getMoreItem();
			// #ifdef H5
			this.$wechat.share({
				link: this.$handlerAppMessagePath()
			});
			// #endif
		});
	},
	onShareAppMessage(e) {
		return {
			path: this.$handlerAppMessagePath()
		};
	},
	methods: {
		// 更新提示
		updateInfo(){
			const updateManager = uni.getUpdateManager();
			updateManager.onUpdateReady(function(res) {
				uni.showModal({
					title: '更新提示',
					content: '新版本已经准备好，是否重启应用？',
					success(res) {
						if (res.confirm) {
							// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
							updateManager.applyUpdate();
						}
					}
				});
			});
		},
		gotoSearch: function() {
			uni.navigateTo({
				url: '../search/search'
			});
		},
		downCallback() {
			this.updateInfo();
			this.moreItems = [];
			Promise.all([this.getHomePage(), this.getMoreItem()])
				.then(() => {
					this.mescroll.resetUpScroll();
				})
				.catch(() => {
					this.mescroll.endErr();
				});
		},
		async upCallback(page) {
			try {
				var Count = await this.getMoreItem();
				this.mescroll.endSuccess(Count, Count > 0);
			} catch (e) {
				// 失败隐藏下拉加载状态
				this.mescroll.endErr();
			}
		},
		async getHomePage() {
			var result = await this.$http.request({ url: 'api/HomePage/GetHomePage' });
			this.swiperItems = result.banners;

			this.navs = result.generalNavs;
			this.pageGeneral = result.homePageItems;
		},
		async getMoreItem() {
			var that = this;
			var result = await this.$http.request({
				url: 'api/HomePage/GetHomePageItemCache',
				data: {
					MaxResultCount: 30,
					SkipCount: that.moreItems.length
				}
			});
			this.moreItems = this.moreItems.concat(result);
			return result.length;
		}
	}
};
</script>

<style lang="scss" scoped>
.common-list {
	width: 95%;
	margin: 0 auto;
	/* margin-bottom: 200rpx; */
}
</style>
